home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / awe2-0_1.lha / awe2-0.1 / Src / RCS / SplayPQ.hP,v < prev    next >
Text File  |  1989-03-22  |  3KB  |  181 lines

  1. head     1.1;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @@;
  7.  
  8.  
  9. 1.1
  10. date     89.03.22.16.16.40;  author grunwald;  state Exp;
  11. branches ;
  12. next     ;
  13.  
  14.  
  15. desc
  16. @@
  17.  
  18.  
  19.  
  20. 1.1
  21. log
  22. @Initial revision
  23. @
  24. text
  25. @// This may look like C code, but it is really -*- C++ -*-
  26. /* 
  27. Copyright (C) 1988 Free Software Foundation
  28.     written by Doug Lea (dl@@rocky.oswego.edu)
  29.  
  30. This file is part of GNU CC.
  31.  
  32. GNU CC is distributed in the hope that it will be useful,
  33. but WITHOUT ANY WARRANTY.  No author or distributor
  34. accepts responsibility to anyone for the consequences of using it
  35. or for whether it serves any particular purpose or works at all,
  36. unless he says so in writing.  Refer to the GNU CC General Public
  37. License for full details.
  38.  
  39. Everyone is granted permission to copy, modify and redistribute
  40. GNU CC, but only under the conditions described in the
  41. GNU CC General Public License.   A copy of this license is
  42. supposed to have been given to you along with GNU CC so you
  43. can know your rights and responsibilities.  It should be in a
  44. file named COPYING.  Among other things, the copyright notice
  45. and this notice must be preserved on all copies.  
  46. */
  47.  
  48.  
  49. #ifndef _<T>SplayPQ_h
  50. #define _<T>SplayPQ_h 1
  51.  
  52. #include "<T>PQ.h"
  53.  
  54. #ifndef _<T>SplayNode
  55. #define _<T>SplayNode 1
  56.  
  57. struct <T>SplayNode
  58. {
  59.   <T>SplayNode*   lt;
  60.   <T>SplayNode*   rt;
  61.   <T>SplayNode*   par;
  62.   <T>             item;
  63.   <T>SplayNode(<T&> h, <T>SplayNode* l=0, <T>SplayNode* r=0);
  64.  ~<T>SplayNode();
  65.  
  66.   <T>SplayNode *operator new(long);
  67.   void operator delete(void *);
  68. };
  69.  
  70.  
  71. inline <T>SplayNode::<T>SplayNode(<T&> h, <T>SplayNode* l=0, <T>SplayNode* r=0)
  72. {
  73.   item = h;
  74.   lt = l;
  75.   rt = r;
  76.   par = 0;
  77. }
  78.  
  79. inline <T>SplayNode::~<T>SplayNode() {}
  80.  
  81. typedef <T>SplayNode* <T>SplayNodePtr;
  82.  
  83. #endif
  84.  
  85. class <T>SplayPQ : public <T>PQ
  86. {
  87. protected:
  88.   <T>SplayNode*   root;
  89.  
  90.   <T>SplayNode*   leftmost();
  91.   <T>SplayNode*   rightmost();
  92.   <T>SplayNode*   pred(<T>SplayNode* t);
  93.   <T>SplayNode*   succ(<T>SplayNode* t);
  94.   void            _kill(<T>SplayNode* t);
  95.   <T>SplayNode*   _copy(<T>SplayNode* t);
  96.  
  97. public:
  98.                   <T>SplayPQ();
  99.                   <T>SplayPQ(<T>SplayPQ& a);
  100.                   ~<T>SplayPQ();
  101.  
  102.   Pix           enq(<T&> item);
  103.   <T>           deq(); 
  104.  
  105.   <T>&          front();
  106.   void          del_front();
  107.  
  108.   int           contains(<T&> item);
  109.  
  110.   void          clear(); 
  111.  
  112.   Pix           first(); 
  113.   Pix           last(); 
  114.   void          next(Pix& i);
  115.   void          prev(Pix& i);
  116.   <T>&          operator () (Pix i);
  117.   void          del(Pix i);
  118.   Pix           seek(<T&> item);
  119.  
  120.   int           OK();                    // rep invariant
  121. };
  122.  
  123. inline <T>SplayPQ::~<T>SplayPQ()
  124. {
  125.   _kill(root);
  126. }
  127.  
  128. inline <T>SplayPQ::<T>SplayPQ()
  129. {
  130.   root = 0;
  131.   count = 0;
  132. }
  133.  
  134. inline <T>SplayPQ::<T>SplayPQ(<T>SplayPQ& b)
  135. {
  136.   count = b.count;
  137.   root = _copy(b.root);
  138. }
  139.  
  140. inline Pix <T>SplayPQ::first()
  141. {
  142.   return Pix(leftmost());
  143. }
  144.  
  145. inline Pix <T>SplayPQ::last()
  146. {
  147.   return Pix(rightmost());
  148. }
  149.  
  150. inline void <T>SplayPQ::next(Pix& i)
  151. {
  152.   if (i != 0) i = Pix(succ((<T>SplayNode*)i));
  153. }
  154.  
  155. inline void <T>SplayPQ::prev(Pix& i)
  156. {
  157.   if (i != 0) i = Pix(pred((<T>SplayNode*)i));
  158. }
  159.  
  160. inline <T>& <T>SplayPQ::operator () (Pix i)
  161. {
  162.   if (i == 0) error("null Pix");
  163.   return ((<T>SplayNode*)i)->item;
  164. }
  165.  
  166. inline void <T>SplayPQ::clear()
  167. {
  168.   _kill(root);
  169.   count = 0;
  170.   root = 0;
  171. }
  172.  
  173. inline int <T>SplayPQ::contains(<T&> key)
  174. {
  175.   return seek(key) != 0;
  176. }
  177.  
  178.  
  179. #endif
  180. @
  181.